package string_func

import (
	"bytes"
	"fmt"
	"strings"
)

func StrFuncShow() {
	// 变量短声明方式
	originStr := ",这里有好大一个世界 > Hello world "

	fmt.Println("原始字符串：", originStr)
	fmt.Println("字符串长度：", len(originStr))
	fmt.Println("分割字符串：", strings.Split(originStr, ">"))
	fmt.Println("替换字符串：", strings.Replace(originStr, "l", "a", 100))
	fmt.Println("字符串元素包含：", strings.Contains(originStr, "Hello"))
	fmt.Println("去除首位空格：", strings.TrimSpace(originStr))
	fmt.Println("去除首位特定字符：", strings.Trim(originStr, ","))
	fmt.Println("字符串前缀判断：", strings.HasPrefix(originStr, ","))
	fmt.Println("字符串后缀判断：", strings.HasSuffix(originStr, ","))
	fmt.Println("字符串元素首次出现位置：", strings.Index(originStr, "l"))
	fmt.Println("字符串元素末次出现位置：", strings.LastIndex(originStr, "l"))
	appendStr := "追加字符Yes"
	originStr = strings.Join([]string{originStr}, appendStr)
	fmt.Println("追加后字符串：", originStr)
}

func ByteFuncShow() {
	s := "baidu.com有事找度娘"
	for i := 0; i < len(s); i++ { //byte
		fmt.Printf("%v(%c) ", s[i], s[i])
	}
	fmt.Println()
	for _, r := range s { //rune
		fmt.Printf("%v(%c) ", r, r)
	}
	fmt.Println()
}

func operatorPerformance(n int, originStr string) string {
	s := ""
	for i := 0; i < n; i++ {
		s += originStr
	}
	return s
}

func fmtPerformance(n int, originStr string) string {
	s := ""
	for i := 0; i < n; i++ {
		s = fmt.Sprintf("%s%s", s, originStr)
	}
	return s
}

func joinPerformance(n int, originStr string) string {
	s := ""
	for i := 0; i < n; i++ {
		s += strings.Join([]string{s}, originStr)
	}
	return s
}

func builderPerformance(n int, originStr string) string {
	var build strings.Builder
	for i := 0; i < n; i++ {
		build.WriteString(originStr)
	}
	return build.String()
}
func bufferPerformance(n int, originStr string) string {
	var buffer bytes.Buffer
	for i := 0; i < n; i++ {
		buffer.WriteString(originStr)
	}
	return buffer.String()
}

func Split(s, sep string) []string {
	i := strings.Index(s, sep)
	result := make([]string, 0, strings.Count(s, sep)+1)
	for i > -1 {
		result = append(result, s[:i])
		s = s[i+1:]
		i = strings.Index(s, sep)
	}
	result = append(result, s)
	return result
}
